<?php

/*
	info: 用户预定
*/

define('IS_MEMBER_CHECK', TRUE);
require '../common.inc.php';

global_CheckPost(
	array(
		array('siteid,sid','int','@>0',$weblang['api_notHaveSite'],1),
		array('peoplenum,','int','@>0',$weblang['api_peopleError'],1),
));

if (!empty($webglobal['CPM']))
{
	$return['code'] = 1;
	$return['msg'] = reset($webglobal['CPM']);
	api_EchoReturn();
}

$webglobal['CPP']['id'] = api_GetTopActivityID();
$webglobal['codeinfo'] = $db->GetOne("SELECT U.id AS codeid,U.sid,U.peoplenum,S.* FROM promotion_user U LEFT JOIN promotion_site S ON (U.sid=S.id) WHERE U.uid='{$user['m_uid']}' AND U.pid='{$webglobal['CPP']['id']}'");
if (empty($webglobal['codeinfo']))
{
	api_EchoReturn($weblang['api_codeError']);
}

//时间临近不能转出
if ($webglobal['codeinfo']['peoplenum']>0 && !empty($webglobal['codeinfo']['id']) && $webglobal['codeinfo']['begintime']-$webserver['timestamp']<$webglobal['lastedittime'])
{
	api_EchoReturn($weblang['api_isReservedTimeOut']);
}

$webglobal['datainfo'] = $db->GetOne("SELECT * FROM promotion_site WHERE id='{$webglobal['CPP']['siteid']}' AND pid='{$webglobal['CPP']['id']}'");
if (empty($webglobal['datainfo']))
{
	api_EchoReturn($weblang['api_notHaveSite']);
}
if ($webglobal['datainfo']['maxpeoplenum']>0 && $webglobal['datainfo']['curpeoplenum']+$webglobal['CPP']['peoplenum']>$webglobal['datainfo']['maxpeoplenum'])
{
	api_EchoReturn($weblang['api_peopleMaxError']);
}

if ($webglobal['codeinfo']['peoplenum'] > 0)
{
	$db->Update('promotion_site', "curpeoplenum=curpeoplenum-{$webglobal['codeinfo']['peoplenum']}", "id='{$webglobal['codeinfo']['sid']}'");
}
$db->Update('promotion_site', "curpeoplenum=curpeoplenum+{$webglobal['CPP']['peoplenum']}", "id='{$webglobal['CPP']['siteid']}'");
$webglobal['CPD']['addtime'] = $webserver['timestamp'];
$db->Update('promotion_user', '', "id='{$webglobal['codeinfo']['codeid']}'");

$return['code'] = 0;
api_EchoReturn();
